package com.atguigu.utils;

import com.google.common.base.CaseFormat;
import org.apache.commons.beanutils.BeanUtils;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:GaoFei
 * @Description:
 * @Date:Created in 18:11
 * @Modified By:
 */
public class JDBCUtil {
    public static <T> List<T> query(Connection connection,String sql,Class<T> clz,boolean isToCamle) throws SQLException, IllegalAccessException, InstantiationException, InvocationTargetException {
        ArrayList<T> resultList = new ArrayList<>();
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();

        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        while (resultSet.next()){
            T t = clz.newInstance();
            for (int i = 0; i < columnCount; i++) {
                String catalogName = metaData.getColumnName(i + 1);
                if (isToCamle){
                    catalogName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL,catalogName);
                }
                Object catalogNameValue = resultSet.getObject(i + 1);
                BeanUtils.setProperty(t,catalogName,catalogNameValue);
            }
            resultList.add(t);
        }
        return resultList;
    }

//    public static void main(String[] args) throws SQLException, IllegalAccessException, InvocationTargetException, InstantiationException {
//        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis","root","admin");
//        String sql = "select id,title,author,create_time,views from mybatis.blog";
//        List<Blog> query = query1(connection, sql, Blog.class, true);
//        for (Blog blog : query) {
//            System.out.println(blog);
//        }
//    }

    private static <T> List<T> query1(Connection connection,String sql,Class<T> clz,boolean isToCamle) throws SQLException, IllegalAccessException, InstantiationException, InvocationTargetException {
        ArrayList<T> resultList = new ArrayList<>();
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            T t = clz.newInstance();
            Field[] fields = t.getClass().getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                String name = fields[i].getName();
                if (isToCamle){
                    name = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE,name);
                }
                Object value = resultSet.getObject(name);
                name = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL,name);
                BeanUtils.setProperty(t,name,value);
            }
            resultList.add(t);
        }
        return resultList;
    }
    }
